package utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResolveUtil {
	private static InputStream inputStream;
	private static OutputStream outputStream;
	private static BufferedReader bufReader;
	private static BufferedWriter bufWrite;
	
	/**
	 * 用于存储解析后的所有ip ， 为去重提供支持
	 */
	ArrayList<String> arr = new ArrayList<String>();
	
	/**
	 * IP去重方法
	 * @param ip 传入ip进行去重
	 * @return 如果没有重复，返回ip ， 否则返回null
	 */
	public String removal(String ip){
		if(arr.isEmpty()){
			arr.add(ip);
			return ip;
		}else {
			Iterator<String> iterator = arr.iterator();
			while(iterator.hasNext()){
				System.out.println("测试代码—：：———————集合执行———————————集合执行—————————集合执行———————————");
				String nextIp = iterator.next();
				if(ip.equals(nextIp)){
					return null;
				}
			}
			arr.add(ip);
			return ip;
		}
	}
	
	/**
	 * 传入要解析的文本，匹配其中的IP地址并返回
	 * @param contnet 
	 * @return 匹配成功返回ip地址 ，否则返回null
	 */
	public String getDomain(String contnet) {
        String reg = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
        Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(contnet);
        boolean blnp = m.find();
        if (blnp == true) {
            return m.group(0);
        }
        return null;
    }
	
	
	/**
	 * 缓冲字符流读写，解析文件中的ip地址
	 */
	public void resolveLog3() {
		try {
			File file = new File(
					"C:\\Users\\lenovo\\Desktop\\practice\\log.log");
			File file2 = new File(
					"C:\\Users\\lenovo\\Desktop\\practice\\log2.log");
			if (file.exists() && file.isFile()) {
				bufReader = new BufferedReader(new FileReader(file));
				bufWrite = new BufferedWriter(new FileWriter(file2));

				String line = null;

				while ((line = bufReader.readLine()) != null) {

					// 测试
					System.out.println(line);
					System.out.println("测试代码；；；执行了：：：：：：：：：：：：");
					//解析IP
					String ip = getDomain(line);
					
					//去重
					String trueIp = removal(ip);
					
					if (trueIp != null) {
						System.out.println("测试代码；；；执行了——————————————————");
						bufWrite.write(("\r\n" + trueIp));
					}
				}
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				bufWrite.close();
				bufReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
	
	/**
	 * 缓冲字节流读写
	 *//*
	public static void resolveLog2(){
		try {
			File file = new File("C:\\Users\\lenovo\\Desktop\\practice\\log.log");
			File file2 = new File("C:\\Users\\lenovo\\Desktop\\practice\\log2.log");
			
			bufin = new BufferedInputStream(new FileInputStream(file));
			bufout = new BufferedOutputStream(new FileOutputStream(file2));
			
			int len = 0 ;
			byte[] buf = new byte[1024];
			
			while((len = bufin.read(buf))!=-1){
				String content = new String(buf,0,len);
				System.out.println("结果"+content);
				
				//测试
				System.out.println("执行了：：：：：：：：：：：：");
				
				String ip = getDomain(content);
				if(ip != null){
					System.out.println("执行了——————————————————");
					bufout.write(("\r\n"+ip).getBytes());
				}
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				bufout.close();
				bufin.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
*/
	/**
	 * 字节流读写  测试练习
	 */
	public static void resolveLog1(){
		try {
			File file = new File("C:\\Users\\lenovo\\Desktop\\practice\\log.log");
			File file2 = new File("C:\\Users\\lenovo\\Desktop\\practice\\log2.log");
			inputStream = new FileInputStream(file);
			outputStream = new FileOutputStream(file2);
			byte[] buf = new byte[1024];
			int length = 0 ;
//			int length = inputStream.read(buf);
			while((length = inputStream.read(buf))!= -1){
				//测试
				String content = new String(buf,0,length);
				System.out.println("content:::"+content);
				
				//边读边写
				outputStream.write(buf, 0, length);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				inputStream.close();
				outputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
}
